package isg3.data;

import isg3.domain.Concurso;
import isg3.domain.Evento;
import isg3.domain.Usuario;
import isg3.utils.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCUsuariosPorEventoDAO implements IUsuariosPorEventoDAO {
private Connection conn;
	
	public JDBCUsuariosPorEventoDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public List selectAllUsuarios(String tipo, String OIDEvento) {
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM  usuarios INNER JOIN usuariosporevento ON usuarios.OIDUsuario = usuariosporevento.usuarioOID WHERE (usuarios.tipo = ? AND usuariosporevento.eventoOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, tipo);
            stmt.setString(2, OIDEvento);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Usuario temp = new Usuario();
                temp.setNombre(result.getString("nombre"));
                temp.setApellidos(result.getString("apellidos"));
                temp.setIdUsuario(result.getString("OIDUsuario"));
                temp.setUsuario(result.getString("user"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
        
        	
	}
	
	public void insert(String idE, String idU, String tarea){
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO usuariosporevento ( OIDUpe, eventoOID, usuarioOID, tarea) VALUES (?, ?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, idE);
            stmt.setString(3, idU);
            stmt.setString(4, tarea);
    

            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	
	public List selectEventos(String idU){
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM   eventos INNER JOIN usuariosporevento ON eventos.OIDEvento = usuariosporevento.eventoOID WHERE (usuariosporevento.usuarioOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, idU);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Evento temp = new Evento();
                temp.setTituloEvento(result.getString("titulo"));
                temp.setDescripcionEvento(result.getString("descripcion"));
                temp.setEstadoEvento(result.getString("estado"));
                temp.setIdEvento(result.getString("OIDEvento"));
                temp.setFecha(result.getDate("fechaEven"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
        
        	
	}
	
	public String selectTarea(String usuarioOID, String eventoOID){
		PreparedStatement stmt = null;
        ResultSet result = null;
        String tarea=null;
        
        try {

            String sql = "SELECT * FROM   usuariosporevento WHERE (usuarioOID = ? AND eventoOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, usuarioOID);
            stmt.setString(2, eventoOID);
            stmt.executeQuery();
            result = stmt.executeQuery();

            result.next();
        
            tarea=result.getString("tarea");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally { 
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return tarea;
        
	}

}
